package com.music.service.impl;

import com.music.dao.CommentMapper;
import com.music.dao.MessageMapper;
import com.music.dao.SingerMapper;
import com.music.dao.UserMapper;
import com.music.entity.Comment;
import com.music.entity.Message;
import com.music.entity.User;
import com.music.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public List<Message> getByMyUserId(String userId) {
        List<Message> messages=messageMapper.queryByMyUserId(userId);

        if (!messages.isEmpty()){
            for (Message message:messages) {
                User myUser=userMapper.findByUserId(message.getMyUserId());
                User otherUser=userMapper.findByUserId(message.getOtherUserId());
                List<Comment>comments =commentMapper.queryChildrenById(message.getMessageText());
                if (!comments.isEmpty()){
                    for (Comment comment:comments) {
                        if (comment.getUserId().equals(message.getMyUserId())){
                            message.setComment(comment);
                        }
                    }
                }
//                log.debug("________"+comment);
                message.setMyUser(myUser);
                message.setOtherUser(otherUser);
            }
        }
        return messages;
    }

    @Override
    public boolean addMessage(Message message) {
        return messageMapper.addMessage(message)>0;
    }

    @Override
    public boolean updataMessage(String userId) {
        return messageMapper.updataMessage(userId)>0;
    }
}
